$(function () {

    initData();

    $("#editTestCaseBtn").click(function(){
        //参数校验
        var url = $("#url").val();
        var teamId = $("#teamId").val();
        var projectId = $("#projectId").val();

        if(teamId == ""){
            window.wxc.xcConfirm("请选择团队!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(projectId == "-1"){
            window.wxc.xcConfirm("请选择项目!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        if(url == ""){
            window.wxc.xcConfirm("url不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
            return;
        }
        $.ajax({
            url: '/lego/updateTestCase',
            type: 'POST',
            data:$("#testCaseForm").toMultiJSONString(),
            contentType:'application/json;charset=UTF-8',
            success: function (data) {
                if (data && data.code == 200) {
                    var op = {
                        title: "提示信息",
                        btn: window.wxc.xcConfirm.btnEnum.ok,
                    }
                    if(data.msg == "更新成功"){
                        window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.success, op);
                    }else {
                        window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.error, op);
                    }
                }
            }
        });
    });
});

function initData(){
    var caseId = $("#caseId").val();

    $.ajax({
        url: '/lego/getTestCaseDetail',
        type: 'GET',
        data:{"caseId":caseId},
        success: function (data) {
            if (data && data.code == 200) {
                var testCase = data.data;

                $("#teamId").val(testCase.teamId);
                $("#projectId").val(testCase.projectId);
                $("#url").val(testCase.url);
                $("#requestType").val(testCase.requestType);
                $(".jsonPathCheckPoint").val(testCase.jsonPathCheckPoint);

                //如果用例已删除,就不展示执行按钮
                if(testCase.status == 0){
                    $("#executeTestCaseBtn").css("display","none");
                }

                $.ajax({
                    url: '/lego/getProjectDetail',
                    type: 'GET',
                    data: {"projectId":testCase.projectId},
                    success: function (data) {
                        if (data && data.code == 200) {
                            $("#ipAndPort").html(data.data.ip + ":" + data.data.port);
                        }
                    }
                });

                //如果该用例有请求头,则展示该请求头
                var headerKeys = testCase.headerKeys;
                var headerValues = testCase.headerValues;
                if(headerKeys != null){
                    if(headerKeys == ""){
                        $("#headerIndex").val(0);
                        $("#headerShowBtn").attr("class","glyphicon glyphicon-chevron-down").html("展开");
                        $("#inputHeaderDiv").css("display","none");
                    }else {
                        $("#inputHeaderDiv").css("display","block");
                        $("#headerIndex").val(1);
                        $("#headerShowBtn").attr("class","glyphicon glyphicon-chevron-up").html("收缩");
                    }

                    var headerKeysAttr = headerKeys.split(",");
                    var headerValueAttr = headerValues.split(",");
                    var size = headerKeysAttr.length;


                    //先给表格的第一行赋值
                    $("#headerKeys").val(headerKeysAttr[0]);
                    $("#headerValues").val(headerValueAttr[0]);

                    //如果还有数据,再加行并赋值
                    for(var i=1;i<size;i++){
                        var trHtml = "<tr>\n" +
                            "         <td><input type=\"text\" class=\"form-control\" name=\"headerKeys\" value=" + headerKeysAttr[i] + "></td>\n" +
                            "         <td><input type=\"text\" class=\"form-control\" name=\"headerValues\" value=" + headerValueAttr[i] + "></td>\n" +
                            "         <td>\n" +
                            "             <button class=\"btn btn-default\" type=\"button\" onclick=\"deleteHeader(this)\">\n" +
                            "                 <span class=\"glyphicon glyphicon-plus\" aria-hidden=\"true\"></span>delete\n" +
                            "             </button>\n" +
                            "         </td>\n" +
                            "     </tr>";
                        var rows = $("#inputHeaderTable").find("tr").length;
                        $("#inputHeaderTable tr").eq(rows-1).after(trHtml);
                    }
                }

                //如果该用例有请求参数,则展示请求参数
                var paramKeys = testCase.paramKeys;
                var paramValues = testCase.paramValues;
                var requestType = testCase.requestType;
                if(paramKeys != null){
                    if(requestType == "GET"){
                        //如果是get请求
                        if(paramKeys == ""){
                            //如果请求参数为null
                            $("#paramsIndex").val(0);
                            $("#paramsShowBtn").attr("class","glyphicon glyphicon-chevron-down").html("展开");
                            $("#paramsKVBodyDiv").css("display","none");
                        }else {
                            //如果有请求参数
                            $("#paramsIndex").val(1);
                            $("#paramsShowBtn").attr("class","glyphicon glyphicon-chevron-up").html("收缩");
                            $("#paramsKVBodyDiv").css("display","block");
                        }

                        var paramKeysAttr = paramKeys.split(",");
                        var paramValueAttr = paramValues.split(",");
                        var size = paramKeysAttr.length;

                        //先给表格的第一行赋值
                        $("#paramKeys").val(paramKeysAttr[0]);
                        $("#paramValues").val(paramValueAttr[0]);

                        //如果还有数据,再加行并赋值
                        for(var i=1;i<size;i++){
                            var rows = $("#inputParamsTable").find("tr").length;
                            var trHtml = "<tr>\n" +
                                "         <td><input type=\"text\" class=\"form-control\" name=\"paramKeys\" value=" + paramKeysAttr[i] + "></td>\n" +
                                "         <td><input type=\"text\" class=\"form-control\" name=\"paramValues\" value=" + paramValueAttr[i] + "></td>\n" +
                                "         <td>\n" +
                                "             <button class=\"btn btn-default\" type=\"button\" onclick=\"deleteHeader(this)\">\n" +
                                "                 <span class=\"glyphicon glyphicon-plus\" aria-hidden=\"true\"></span>delete\n" +
                                "             </button>\n" +
                                "         </td>\n" +
                                "     </tr>";
                            var rows = $("#inputParamsTable").find("tr").length;
                            $("#inputParamsTable tr").eq(rows-1).after(trHtml);
                        }
                    }else if(requestType == "POST"){
                        //如果是post请求
                        $("#paramsKVDiv").css("display","none");
                        $("#paramsRawDiv").css("display","block");
                        $("#paramRaw").val(testCase.paramRaw);
                    }
                }

            }
        }
    });
}
